/*
 * 0053. 翻转字符串
 * 给定一个字符串，逐个翻转字符串中的每个单词。
 * https://www.lintcode.com/problem/reverse-words-in-a-string/description
 * 
 * 2018.07.13 @jeyming
 */
package reverse_words_in_a_string_0053;

public class Reverse_words_in_a_string_0053 {
	/*
	 * @param s: A string
	 * @return: A string
	 */
	public String reverseWords(String s) {
		// write your code here
		String res = "";
		int cnt = 0;
		for(int i = s.length() - 1; i >= 0; --i) {
			if(s.charAt(i) != ' '){
				++cnt;
			} else if(cnt > 0){
				for(int j = i + 1; j <= i + cnt; ++j) {
					res += s.charAt(j);
				}
				res += " ";
				cnt = 0;
			}
			if((i == 0) && (cnt > 0)) {
				for(int j = i; j <= i + cnt; ++j) {
    				res += s.charAt(j);
    			}
			}
		}
		return res;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
